package com.jushu.storbox;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.adapter.ChildAdapter;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.util.LogUtil;

public class ShowImageActivity extends Activity {

	private final static String LOG_TAG = "ShowImageActivity";

	private GridView mGridView;

	private List<String> list;

	private ChildAdapter adapter;

	private TextView tv_file_browser_title;

	private String title;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogUtil.i(LOG_TAG, "onCreate");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_show_detail);
		ActivityTaskManager.getInstance().putActivity(LOG_TAG,
				ShowImageActivity.this);
		findViews();
		getExtraData();
		setAdapter();
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 101) {
				Bundle bundle = (Bundle) msg.obj;
				Intent intent = new Intent(ShowImageActivity.this,
						LookImageActivity.class);
				intent.putExtras(bundle);
				ShowImageActivity.this.startActivityForResult(intent, 2);
			}
			super.handleMessage(msg);
		}
	};

	private void findViews() {
		tv_file_browser_title = (TextView) findViewById(R.id.tv_file_browser_title);
		mGridView = (GridView) findViewById(R.id.child_grid);
	}

	private void getExtraData() {
		Intent intent = getIntent();
		list = intent.getStringArrayListExtra("data");
		title = intent.getStringExtra("parentDir");
		tv_file_browser_title.setText(null == title ? "" : title);
	}

	private void setAdapter() {
		adapter = new ChildAdapter(this, list, mGridView, handler);
		mGridView.setAdapter(adapter);
	}

	/**
	 * @param view
	 */
	public void uploadOrCancel(View view) {
		switch (view.getId()) {
		case R.id.upload:
			if (adapter.getSelectedFiles().size() <= 0) {
				Toast.makeText(
						ShowImageActivity.this,
						ShowImageActivity.this.getResources().getString(
								R.string.please_selete_image),
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				if (!ShareDriveApplication.getInstance()
						.getWifiController().getNetworkState()) {
					Toast.makeText(
							ShowImageActivity.this,
							ShowImageActivity.this
									.getString(R.string.allfile_search_is_not_vailible),
							Toast.LENGTH_SHORT).show();
				} else {
					Intent data = new Intent();
					Bundle bundle = new Bundle();
					bundle.putStringArrayList("resultUIL",
							adapter.getSelectedFiles());
					data.putExtra("result", bundle);
					setResult(MessageCode.UPLOAD_SELECT_FILE_RESULTCODE, data);
					Toast.makeText(
							ShowImageActivity.this,
							ShowImageActivity.this.getResources().getString(
									R.string.image_successfully_added),
							Toast.LENGTH_SHORT).show();
				}
				ShowImageActivity.this.finish();
			}
			break;

		case R.id.cancel:
			adapter.clearCacheState();
			setResult(-1, null);
			ShowImageActivity.this.finish();
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (resultCode) {
		case 2:
			setResult(2, data);
			ShowImageActivity.this.finish();
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtil.i(LOG_TAG, "onDestroy");
		ActivityTaskManager.getInstance().removeActivity(LOG_TAG);
	}

	public void onBackClick(View v) {
		onBackPressed();
	}
}
